#include "Light.h"

#include "LightConfig.h"

#define LIGHT_AIN AIN(LIGHT_ADC)

/**
 * @brief 光照传感器初始化
 * 
 */
void Light_init()
{
    ADC_init(LIGHT_AIN, PERIOD_32sys, VREFS_VDD);
}

uint16_t Light_directV()
{
    return (ADC_getVoltageSync(LIGHT_ADC));
}

/**
 * @brief 读取光照传感器的强度
 * 
 * @return uint8_t 读取光照强度
 */
uint8_t Light_get()
{
    double adc_value = (double)(ADC_getVoltageSync(LIGHT_ADC));
    double light_result = (adc_value-LIGHT_0)/(LIGHT_100-LIGHT_0)*100;
    if(light_result < 0)
    {
        light_result = 0;
    }
    if(light_result > 100)
    {
        light_result = 100;
    }
    return (uint8_t)light_result;
}
